var {
  ocrUtils,
  textUtils,
  randomUtils,
  timeUtils,
  swipeUtils,
  logUtils,
} = require("../../utils/index");
var Action = require("../../base/Action");
var aWatchVideo = new Action("看视频", {});
function nextVideo() {
  swipeUtils.swipeX(
    ((device.width / 2) * randomUtils.getRandom(90, 100)) / 100,
    ((device.height / 10) * 8 * randomUtils.getRandom(90, 100)) / 100,
    ((device.width / 2) * randomUtils.getRandom(90, 100)) / 100,
    ((device.height / 10) * 2 * randomUtils.getRandom(90, 100)) / 100,
    randomUtils.getRandom(500, 1000, 1)
  );
}
aWatchVideo.isOver = function () {
  var contents = ["重播", "立即关注", "点击进入"];
  if (textUtils.existsOneTextStartsWith(contents)) {
    return true;
  }
  if (ocrUtils.captureExistsOneText(contents)) {
    return true;
  }
  return false;
};
aWatchVideo.setSpan = function (span) {
  this.span = span;
};
aWatchVideo.exec = function () {
  this.testNextVideo();
  var startTime = Date.now();
  var count = 0;
  var span = this.span || 210 * 60 + randomUtils.getRandom(10, 120);
  logUtils.info("exec span: ", span);
  while (true) {
    var interval = randomUtils.getRandom(30, 120);
    this.watchAndCheck(interval);
    if (this.isOver()) {
      logUtils.info("今天的视频看完了！！！");
      sleep(randomUtils.getRandom(1.5, 20, 1000));
      break;
    }
    count++;
    var hasExec = timeUtils.hasExec(startTime);
    if (count % 5 == 0) {
      logUtils.info("已经看了-" + hasExec + "/" + span + "-s");
    }
    if (hasExec >= span) {
      logUtils.info("时间已到，将结束本轮看视频");
      break;
    }
  }
};
aWatchVideo.isOver = function () {
  var contents = ["明日再来", "明天再来"];
  var TextUI = textUtils.existsOneTextStartsWith(contents);
  if (TextUI) {
    return true;
  }
  var textOCR = ocrUtils.captureExistsOneText(contents);
  if (textOCR) {
    return true;
  }
};
aWatchVideo.watchAndCheck = function (interval) {
  logUtils.info("将看：", interval, "s");
  var span = interval;
  var count = 0;
  var startTime = Date.now();
  while (true) {
    sleep(20 * 1000);
    count++;
    var hasExec = timeUtils.hasExec(startTime);
    if (count % 5 == 0) {
      logUtils.info("本次已经看了-" + hasExec + "/" + span + "-s");
    }
    if (hasExec > span) {
      logUtils.info("时间已到，将结束本次看视频");
      break;
    }
    this.randomNextVideo();
  }
};
aWatchVideo.testNextVideo = function () {
  var length = randomUtils.getRandom(1, 3);
  logUtils.info("testNextVideo length: ", length);
  for (var index = 0; index < length; index++) {
    var interval = randomUtils.getRandom(2, 15, 1000);
    logUtils.info("testNextVideo", index + 1, "将看", interval / 1000, "秒");
    sleep(interval);
    nextVideo();
  }
};
aWatchVideo.randomNextVideo = function () {
  var contents = ["重播", "立即关注", "点击进入"];

  if (textUtils.existsOneTextStartsWith(contents)) {
    logUtils.info(1, "将看下一个视频");
    nextVideo();
    return;
  }
  if (ocrUtils.captureExistsOneText(contents)) {
    logUtils.info(2, "将看下一个视频");
    nextVideo();
    return;
  }
  if (randomUtils.possible(90, 100)) {
    logUtils.info(3, "将看下一个视频");
    nextVideo();
    return;
  }

  return false;
};
module.exports = aWatchVideo;
